import { stopCoup } from '../requets/coupFun'
import requestsd from '../utils/request'
import { outActivityList, outAutoActivityLogs } from './getPageMessage'
const doudianBaseUrl = 'https://fxg.jinritemai.com/'
console.log('doudianBaseUrl', doudianBaseUrl)
const conterys = {
    stopCoup: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            const stopStatus = await stopCoup(request.id).catch(err => {
                console.log('停止错误', err)
            });
            console.log('停止结果123456798', stopStatus);
            sendResponse(stopStatus);
            return true
        }
    },
    originHttp: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            const reterDdata = await requestsd({
                baseURL: request.data.baseURL || doudianBaseUrl,
                url: request.data.url || '',
                method: request.data.method || 'get',
                headers: request.data.headers || {},
                data: request.data.data || {},
                param: request.data.param || {}
            }).catch(err => {
                console.log('封装请求错误2', err)
            })
            // console.log('新封装的请求', reterDdata)
            sendResponse(reterDdata);
            return true
        }
    },
    originHttpWin: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // const reterDdata = await requestsd({
            //     baseURL: request.data.baseURL || doudianBaseUrl,
            //     url: request.data.url || '',
            //     method: request.data.method || 'get',
            //     headers: request.data.headers || {},
            //     data: request.data.data || {},
            //     param: request.data.param || {}
            // })
            // console.log('新封装的请求', reterDdata)
            // console.log('新封装的请求235645', request)
            const gters = await getWinquers({
                baseURL: request.data.baseURL || doudianBaseUrl,
                url: request.data.url || '',
                method: request.data.method || 'get',
                headers: request.data.headers || {},
                data: request.data.data || {},
                param: request.data.param || {},
                paramete: request.data.param || {}
            }).catch(err => {
                console.log('封装请求错误', err)
            })
            console.log('新封装的请求00000', gters)
            sendResponse(gters);
            return true
        }
    },
    saveStoreges: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // console.log('stopregeData', request.data)
            window.postMessage(
                {
                    method: "saveLocstros",
                    data: request.data,
                    type_urs: 'wtbg'
                },
                "*"
            )
            sendResponse(true)
            return true
        }
    },
    getNewPrd: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            window.postMessage(
                {
                    method: "getNewPrdHoter",
                    data: request.data,
                    type_urs: 'wtbg'
                },
                "*"
            )
            sendResponse(true)
        }
    },
    send_in_dly_script_content: {
        // 代理对应页面脚本的工作
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // console.log('doulingyang--mini出入00')
            window.postMessage(
                {
                    method: "send_dly_all",
                    data: request.data,
                    type_urs: 'wtbg',
                    api: request.api || ''
                },
                "*"
            )
            sendResponse(true)
        }
    },
    send_in_dly_script_eval: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {

            const script = document.createElement('script')
            script.innerHTML = request.script
            document.body.appendChild(script)

            window.postMessage(
                {
                    method: "send_dly_all",
                    data: { code: 200 },
                    type_urs: 'wtbg',
                    api: request.api || ''
                },
                "*"
            )
            sendResponse(true)
        }
    },
    dly_reload_autodata: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            outActivityList()
            outAutoActivityLogs()
            sendResponse(true)
        }
    },
    send_in_dly_call_script_function: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // console.log('   对应页面读到了请求', request, sender, sendResponse)
            window.postMessage(
                {
                    // method: "send_dly_all",
                    type: 'wt_bgRequest',
                    body: request.body,
                    api: request.api || '',
                    for_tab_id: request.for_tab_id || '',
                    createTaskIdStr: request.createTaskIdStr || ''
                },
                "*"
            )
            sendResponse && sendResponse(true)
        }

    },
    send_in_dly_call_script_function_callback: {
        allow: { script: true },
        exec: async (request, sender, sendResponse) => {
            // console.log('   请求放回的 准备放到页面去', request, sender, sendResponse)
            window.postMessage(
                {
                    method: "send_dly_all",
                    type: 'wt_bgRequest_topage',
                    data: request.data,
                    api: request.api || '',
                    createTaskIdStr: request.createTaskIdStr || ''
                },
                "*"
            )
            sendResponse && sendResponse(true)
        }
    }
}


let requestmap = []
const getWinquers = function (body) {
    return new Promise((resolve, reject) => {
        requestmap.push({
            body,
            resolve,
            reject
        })
        window.postMessage(
            {
                method: "getHttpWins",
                body
            },
            "*"
        )
        window.addEventListener("message", function (event) {
            if (
                event.data &&
                event.data.method == "setHttpWins" &&
                event.data.data
            ) {
                const spilecindex = requestmap.findIndex((item) => {
                    // console.log('item.body', item.body, event.data)
                    return (
                        JSON.stringify(item.body) ==
                        JSON.stringify(event.data.data.body)
                    )
                })
                if (spilecindex !== -1) {
                    const rutyer = requestmap.splice(spilecindex, 1)
                    if (rutyer && rutyer.length) rutyer[0].resolve(event.data.data)
                }
            }
        })
    })
}
export default conterys